﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.Framework;
using CNVP.JShop.UI;
using CNVP.Framework.Utils;

namespace CNVP.JShop.Web.Member
{
    public partial class GetAddressEdit : UserPage
    {
        BLL.CNVP_GetAddress bll = new BLL.CNVP_GetAddress();
        Model.CNVP_GetAddress model = new Model.CNVP_GetAddress();
        public int id = 0;
        public string Province = "0", City = "0", Area = "0";
        public int PageIndex = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ID"])&& Public.IsNumber(Request.QueryString["ID"]))
            {
                id = Convert.ToInt32(Request.QueryString["ID"]);
            }
            if (!IsPostBack)
            {
                ProvinceBind();
                if (id > 0)
                {
                    lbTitle.Text = "修改";
                    DataInfo();
                }
                else
                {
                    lbTitle.Text = "添加";
                }
                ddlProvince.Attributes.Add("onchange", "ProvinceChange()");
            }

        }

        private void DataInfo()
        {
            model = bll.GetSingle_CNVP_GetAddressById(id);
            if (model != null)
            {
                txtTrueName.Text = model.TrueName;
                txtGetAddress.Text = model.GetAddress;
                txtMobilePhone.Text = model.MobilePhone;
                txtPhone.Text = model.Phone;
                txtZipCode.Text = model.ZipCode;
                ddlProvince.SelectedValue = model.Province;
                Province = model.Province;
                City = model.City;
                Area = model.Area;
                if (model.IsTolerant == 1)
                {
                    cbIsTolerant.Checked = true;
                }
                else
                {
                    cbIsTolerant.Checked = false;
                }
            }
        }

        private void ProvinceBind()
        {
            IList<Model.CNVP_Province> model = new BLL.CNVP_Province().GetAll_CNVP_Province();
            foreach (Model.CNVP_Province p in model)
            {
                ddlProvince.Items.Add(new ListItem(p.Name, p.Code));
            }
        }

        protected void btSaveAddress_Click(object sender, EventArgs e)
        {
            try
            {
                model.Province = ddlProvince.SelectedValue;
                model.City = Request.Form["ListCity"];
                model.Area = Request.Form["ListArea"];
                model.GetAddress = txtGetAddress.Text;
                model.TrueName = txtTrueName.Text;
                model.MobilePhone = txtMobilePhone.Text;
                model.Phone = txtPhone.Text;
                model.ZipCode = txtZipCode.Text;
                if (cbIsTolerant.Checked == true)
                {
                    bll.Update_CNVP_GetAddressWhereOne(CookieUserID);
                    model.IsTolerant = 1;
                }
                else
                {
                    model.IsTolerant = 0;
                }
                if (id > 0)
                {
                    model.ID = id;
                    bll.Update_CNVP_GetAddress(model);
                    BasePage.MessageBox("修改成功！", "GetAddressList.aspx?Page=" + Request.QueryString["Page"] + "");
                    DataInfo();
                }
                else
                {
                    model.UsersID = CookieUserID;
                    bll.Add_CNVP_GetAddress(model);
                    BasePage.MessageBox("添加成功！", "GetAddressList.aspx");
                }
            }
            catch
            {
                MessageUtils.ShowPre("失败！");
            }
        }
    }
}